using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{
    public partial class LaunchAttack : Form
    {
        private Attack attack;
        private bool lastWarningIssued=false;
        private bool canClose = false;

       

        public LaunchAttack(Attack attack)
        {
            InitializeComponent();

            this.attack = attack;
            villageDisplay_AttackFrom.VillageToDisplay = attack.AttackingVillage;
            villageDisplay_Target.VillageToDisplay = attack.TargetVillage;
            lblAttackingUnit.Text = attack.AttackingUnit.Name;
            lblLaunchTime.Text = App.FormatTWEventTime(attack.TimeLaunched);
            txtDescription.Text = attack.Description;
            txtTargetDesc.Text = attack.Target.Description;
        }

        //public LaunchAttack()
        //{
        //    InitializeComponent();

        //}

        enum State
        {
            fadein,
            showing,
            //fadeout,
            done
        }
        private State showingState = State.fadein;

        private void timerOpacity_Tick(object sender, EventArgs e)
        {
            switch (showingState)
            {
                case State.fadein:
                    if (this.Opacity >= 1)
                    {
                        showingState = State.showing;                        
                        timerOpacity.Interval = 2000;
                    }
                    else
                    {
                        this.Opacity = this.Opacity + 0.02;
                    }
                    break;
                case State.showing:
                    Fade_StopFadeIn();
                    break;
                //case State.fadeout:
                //    if (this.Opacity <= 0.1)
                //    {
                //        this.TopMost = false;
                //        this.WindowState = FormWindowState.Minimized;
                //        this.Opacity = 1;
                //        timerOpacity.Enabled = false;
                //    }
                //    else
                //    {
                //        this.Opacity = this.Opacity - 0.02;
                //    }
                //    break;
                //case State.done:
                //    timerCountDown.Enabled = false;
                //    break;
            }
        }

        //private void Fade_StartFadeOut()
        //{
        //    showingState = State.fadeout;
        //    timerOpacity.Interval = 50;
        //    timerOpacity.Enabled = true;

        //}

        private void Fade_StopFadeIn()
        {
            if (showingState != State.done)
            {
                this.Opacity = 1;
                showingState = State.showing;
                timerOpacity.Enabled = false;
                this.TopMost = false;
                showingState = State.done;
            }
        }

        bool oneMinuteWarningSounded = false;
        bool timeExpiredWarningSounded = false;

        private void SoundBeep(int numberOfBeeps)
        {
            if (!WindowsApplication4.Properties.Settings.Default.MuteSound)
            {
                for (int i = 1; i <= numberOfBeeps; i++)
                {
                    Console.Beep(800, 200);
                }
            }
        }

        private void timerCountDown_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now < attack.TimeLaunched)
            {
                TimeSpan timeTillLaunch = attack.TimeLaunched - DateTime.Now;

                lblLaunchIn.Text = App.FormatTWDuration(timeTillLaunch);
                this.Text = "Attack in " + lblLaunchIn.Text;
                if (timeTillLaunch.TotalMinutes <= 1)
                {
                    btnSnooze.Enabled = false;
                    lblLaunchIn.ForeColor = Color.DarkGoldenrod;

                    if (!oneMinuteWarningSounded)
                    {
                        oneMinuteWarningSounded = true;

                        SoundBeep(2);
                    }

                    if (!lastWarningIssued)
                    {
                        this.Visible = true;
                        this.WindowState = FormWindowState.Normal;
                        this.TopMost = true;
                        this.TopMost = false;
                        this.Activate();

                        lastWarningIssued = true;
                    }
                }
                else
                {
                    lblLaunchIn.ForeColor = Color.Green;
                }
            }
            else
            {
                btnSnooze.Enabled = false;
                lblLaunchIn.ForeColor = Color.Crimson;
                lblLaunchIn.Text = "Time expired!!";
                this.Text = "Attack - time expired!!";

                if (!timeExpiredWarningSounded)
                {
                    timeExpiredWarningSounded = true;
                    SoundBeep(3);
                }

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            attack.Launched = true;
            canClose = true;
            this.Close();
        }

        private void Snooze() 
        {
            if (DateTime.Now < attack.TimeLaunched)
            {
                TimeSpan timeTillLaunch = attack.TimeLaunched - DateTime.Now;
                if (timeTillLaunch.TotalMinutes > 1)
                {
                    this.Visible = false;
                }
            }

        }



        private void LaunchAttack_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing && ! canClose)
            {
                e.Cancel = true;
                Snooze();
            }
        }

        private void lblLaunchIn_Click(object sender, EventArgs e)
        {

        }

        private void btnSnooze_Click(object sender, EventArgs e)
        {
            Snooze();
        }

        private void LaunchAttack_Load(object sender, EventArgs e)
        {
            SoundBeep(1);
        }

        private void LaunchAttack_MouseEnter(object sender, EventArgs e)
        {
            Fade_StopFadeIn();
        }

        private void LaunchAttack_MouseLeave(object sender, EventArgs e)
        {
      //      Fade_StartFadeOut();
        }

        private void txtTargetDesc_TextChanged(object sender, EventArgs e)
        {

        }

    }
}